window.onerror = function(){
    return true;
}
var showymz = false;

(function(){
    // 登录错误提示
    var error_box = $('#error_box');
    var arrow = error_box.find('a.arrow');
    arrow.click(function(){
        error_box.hide(150);
    });
    
    var errorTip = $('#error_tip');
    var traces = new at_trace();
    traces.setWidth(350);
    var l_box = $('#login_box');
    var yzm = l_box.find('.yzm,.yzm1');//验证码
    var input = l_box.find('.at_text input');
    //设置输入框的样式
    input.focus(function(){
        $(this).addClass('focus');
        $(this).parent('.at_text').attr('class', 'at_text t_l_i');
    }).blur(function(){
        var val = $(this).val();
        if (val == '' || val == '用户名/手机/邮箱') {
            $(this).removeClass('focus');
            $(this).parent('.at_text').attr('class', 'at_text t_l_d');
        }
    });
    //判断用户名/密码是否填写
    function judge(){
        var user_name = l_box.find('#user_name');
        var user_pas = l_box.find('#user_pas');
        if (user_name.val() == '' || user_name.val() == '用户名/手机/邮箱') {
            errorTip.show().find('p').html('<i></i>用户名不能为空!');
            user_name.focus();
            return false;
        }
        if (user_pas.val() == '') {
            errorTip.show().find('p').html('<i></i>密码不能为空!');
            user_pas.focus();
            return false;
        }
        
        if (showymz) {
            if ($('#catcha').val() == '') {
                errorTip.show().find('p').html('<i></i>验证码不能为空!');
                $('#catcha').focus();
                return false;
            }
        }
    }
    
    $('#l_sub').click(function(){
        $('#login_box').submit();
        return false;
    })
    
    $('#login_box').submit(function(){
		if (judge() == false) {
        	return false;
        }
        $('#l_sub').addClass('l_sub_hover').html('<u></u>加载中');
        var error = $('#error_tip');
        var error_p = error.find('p');
        $.ajax({
            url: path + '/user/dologin',
            data: $(this).serialize(),
            success: function(json){
                if (json.state == 1) {
                	var cookies = json.cookie;
                	var c = cookies.split('|');
                	$.cookie("nickname", c[0],{path:'/'});
                	$.cookie("userid", c[1],{path:'/'});
                	$.cookie("msgcount", c[2],{path:'/'});
                    window.location.href = json.url;
                }
                else 
                    if (json.state == -2) {//显示错误消息和验证码
                        $('#l_sub').addClass('l_sub_hover').html('<u></u>登录');
                        error.show();
                        error_p.html('<i></i>' + json.msg + '');
                        yzm.show();
                        showymz = true;
                        $('#catcha').val('');
						l_box.find('#user_pas').focus();
                        catchasrc();
                    }
                    else {
                        $('#l_sub').addClass('l_sub_hover').html('<u></u>登录');
                        error.show();
                        error_p.html('<i></i>' + json.msg + '');
                        error_box.show(100);
                        $('#catcha').val('');
						l_box.find('#user_pas').focus();
                        catchasrc();
                    }
            }
        });
		l_box.find('#user_pas').val('');
        return false;
    })
    
    var tips = l_box.find('.tips');
    tips.find('label').hover(function(){
        tips.addClass('tips_on');
    }, function(){
        tips.removeClass('tips_on');
    })
    if (showc == '0') {
        showymz = true;
        yzm.show();
    }
})()

function catchasrc(){
    var url = path+'/login/catcha' + '?n=' + Math.random();
    $('#catchaimg').attr('src', url);
    return false;
}

function showHelp(){
    var activ = new at_trace();
    activ.setContact('寻师宝登录方式', 'df');
    activ.setWidth(500);
    var htmlObj = $('#loginHelp').html();
    activ.appendContact(htmlObj);
    activ.setDisplay('show');
}

$(document).ready(function(){
    var nav_today = $('#nav_today'), ul = nav_today.find('ul'), ul_h = nav_today.height();
    var n = 0;
    var ul_size = ul.size();
    function start(){
        scrollUp();
        if (n == ul_size) {
            nav_today.scrollTop(0);
            n = 0;
        }
    }
    var timer = window.setInterval(function(){
        start();
    }, 2000);
    
    function stop(){
        clearInterval(timer);
    };
    
    function restart(){
        timer = window.setInterval(function(){
            start();
        }, 2000);
    };
    
    function scrollUp(){
        if (nav_today.is(':animated')) {
            return false;
        }
        stop();
        nav_today.animate({
            scrollTop: nav_today.scrollTop() + ul_h
        }, 'normal', function(){
            n++;
            if (n == ul_size) {
                nav_today.scrollTop(0);
                n = 0;
            }
            restart();
        });
    };
    
    function scrollDown(){
        if (nav_today.is(':animated')) {
            return false;
        }
        stop();
        nav_today.animate({
            scrollTop: nav_today.scrollTop() - ul_h
        }, 'normal', function(){
            n--;
            if (n == 0) {
                nav_today.scrollTop(ul_h * ul_size);
                n = ul_size;
            }
            restart();
        });
    }
    nav_today.mouseover(function(){
        stop();
    }).mouseout(function(){
        restart();
    });
    nav_today.scrollTop(0);
    restart();
});
